/**
 * Created by wang on 16-7-1.
 */
var poem_content;
var poem_type;
 var poem_title ;
$(document).ready(function () {
    poem_type = decodeURI(get_parameter("poem_type"));//5言7言
    poem_content = decodeURI(get_parameter("poem_con"));//诗句
    var poem_sens = decodeURI(get_parameter("poem_sens"));//4句8句
     poem_title = decodeURI(get_parameter("poem_title"));//标题
    var poem_heads = decodeURI(get_parameter("poem_heads"));//input输入内容
    //设置页面标题
    document.title='以诗传情'+(poem_title=='爱情'?'赠情人':'赠友人');
    //微信分享data
    var timestamp;//必填，生成签名的时间戳
    var nonceStr;//必填，生成签名的随机串
    var signature;// 必填，签名，见附录1
    var sharelink;//分享链接
    var sharetitle;//分享标题
    var shareimg;//分享图标
    var sharecontent;//分享描述
    var content;
    var words = 5;
    // 根据referrer判断是跳转过来的还是用户点击分享进来的
    if (document.referrer == '') {
    } else {
        $("body").css("background-color", "#333");
        $(".index-wrapper-son").hide();
        $(".index-wrapper").append("<div id='share_div'><img src='image/share.png' class='share_friend'/></div>");
        $("#share_div").css('height', document.body.scrollHeight + 'px');
        $("#share_div").on("click", function () {
            //  $('.shareLabel').css('height', $('.shareDiv').css('height'));
            // $("body").css("background-color", "#fff");
            // $(".index-wrapper-son").show();
            // $("#share_div").remove();
        });
    }
    //标签：height：100%不起效果，用js控制
    $('.shareLabel').css('height', $('.shareDiv').css('height'));
    drawPoem('lover', poem_type, poem_sens, poem_content);
//我也要作诗
    $('#lover_change').click(function () {
        window.location.href = "poem.html?poem_type=" + poem_type + "&poem_sens=" + poem_sens + "&poem_con=" + poem_content + "&poem_title=" + poem_title + "&poem_heads=" + poem_heads;
    });
    share();
});
//写诗
function drawPoem(content, poem_type, poem_sens, poem_content) {
    var poem_arr = [];
    if (poem_type == 5) {
        $("." + content + "-senven-4").hide();
        $("." + content + "-senven-8").hide();
        if (poem_sens == 4) {
            $("." + content + "-five-8").hide();
            $("." + content + "-five-4").show();
            var weui_arr = $("." + content + "-five-4" + " li");
        }
        else if (poem_sens == 8) {
            $("." + content + "-five-4").hide();
            $("." + content + "-five-8").show();
            var weui_arr = $("." + content + "-five-8" + " li");
        }
        for (var i = 0; i < poem_content.length; i++) {
            $(weui_arr[i]).text(poem_content[i]);
        }
    } else if (poem_type == 7) {
        $("." + content + "-five-4").hide();
        $("." + content + "-five-8").hide();
        if (poem_sens == 4) {
            $("." + content + "-senven-8").hide();
            $("." + content + "-senven-4").show();
            var weui_arr = $("." + content + "-senven-4" + " li");
        } else if (poem_sens == 8) {
            $("." + content + "-senven-4").hide();
            $("." + content + "-senven-8").show();
            var weui_arr = $("." + content + "-senven-8" + " li");
        }
        for (var i = 0; i < poem_content.trim().length; i++) {
            $(weui_arr[i]).text(poem_content[i]);
        }
    }
}
//得到地址的数据
function get_parameter(param) {
    var query = window.location.search;
    var iStart = query.indexOf(param);
    var iLen = param.length;
    if (iStart == -1) {
        return "";
    }
    iStart += iLen + 1;
    var iEndAnd = query.indexOf("&", iStart);
    var iEndSplash = query.indexOf("/", iStart);
    if (iEndAnd == -1 && iEndSplash == -1) {
        return query.substring(iStart);
    } else if (iEndAnd > 0) {
        return query.substring(iStart, iEndAnd);
    } else if (iEndSplash > 0) {
        return query.substring(iStart, iEndSplash);
    }
}
//诗句加逗号
function addCharToPoem(sharecontent) {
    // var result;
    // if (sharecontent.replace(/[\r\n]/g, "") == '') {
    //     $.alert("诗句为空");
    // } else {
    //     for(i=0;i<){
    //
    //     }
    //     sharecontent.substr()
    // }
}
//分享
function share() {
    var data = {
        'url': encodeURIComponent(window.location.href)
    };
    var post_data = JSON.stringify(data);
    console.log('post_data' + post_data);
    $.ajax({
        url: 'http://kcloudtest.iflytek.com/zb/signaturedata?data=' + post_data,
        type: 'get',
        success: function (data) {
            $.hideLoading();
            get_data = JSON.parse(data);
            //初始化分享参数
            timestamp = get_data.timestamp;
            nonceStr = get_data.nonceStr;
            signature = get_data.signature;
            sharelink = window.location.href;
            // sharetitle = get_data.title;
            sharetitle = "以诗传情 "+(poem_title=='爱情'?'赠情人':'赠友人');
            shareimg = get_data.img_url;
            sharecontent = '关注躺倒鸭，回复装逼，获取更多神器';
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: 'wx8ef01b828a087354', // 必填，公众号的唯一标识
                timestamp: timestamp, // 必填，生成签名的时间戳
                nonceStr: nonceStr, // 必填，生成签名的随机串
                signature: signature,// 必填，签名，见附录1
                jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
            var title = "关注躺倒鸭，回复“装逼”获得更多神器";
            var desc = "关注躺倒鸭，最牛逼的装逼";
            var url = sharelink;
            var img = 'http://open.weixin.qq.com/qr/code/?username=tangdaoya';

            wx.ready(function () {
                wx.checkJsApi({
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage'
                    ]
                });
                wx.onMenuShareTimeline({
                    title: sharetitle, // 分享标题
                    link: sharelink, // 分享链接
                    imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
                    success: function () {
                        alert('鸭鸭感谢你的分享')
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                        alert('撤销');
                    }
                });
                wx.onMenuShareAppMessage({
                    title: sharetitle,
                    desc: sharecontent,
                    link: sharelink,
                    imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya',
                    trigger: function (res) {

                    },
                    success: function (res) {
                        alert('已分享');
                    },
                    cancel: function (res) {
                        alert('已取消');
                    },
                    fail: function (res) {
                        alert(JSON.stringify(res));
                    }
                });

                wx.onMenuShareQQ({
                    title: sharetitle, // 分享标题
                    desc: sharecontent, // 分享描述
                    link: sharelink, // 分享链接
                    imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                    }
                });

                wx.onMenuShareQZone({
                    title: sharetitle, // 分享标题
                    desc: sharecontent, // 分享描述
                    link: sharelink, // 分享链接
                    imgUrl: 'http://open.weixin.qq.com/qr/code/?username=tangdaoya', // 分享图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                    }
                });
            });
            wx.error(function (res) {

            });
        }, error: function (data) {
            $.alert("error");
        }
    })

}